﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using DrawingLines.Properties;
using ImageSaver;
using PhotoshopFile;

namespace DrawingLines
{
    /// <summary>
    /// The form for drawing. Partial class.
    /// </summary>
    public partial class DrawingForm : Form
    {
        private Graphics _graphics;
        private Bitmap _bitmap;
        private SolidBrush _solidBrush;
        private byte _xSize;

        /// <summary>
        /// Default constructor
        /// </summary>
        public DrawingForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Preparing actions for drawing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DrawingFormLoad(object sender, EventArgs e)
        {
            _xSize = 1;
            _solidBrush = new SolidBrush(Color.Black);
            ToolStripProgressBarColor.ForeColor = Color.Black;
            PanelColor.BackColorChanged += PanelColorForeColorChanged;
            ClearAndSet(new Bitmap(PictureBox.Width, PictureBox.Height));
            _graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, PictureBox.Width, PictureBox.Height);
            PictureBox.Refresh();
            ContextMenuStrip.Items.Add(ToolStripMenuItemColorSettings);
            ContextMenuStrip.Items.Add(ToolStripMenuItemWidthSettings);
        }

        void PanelColorForeColorChanged(object sender, EventArgs e)
        {
            ToolStripProgressBarColor.ForeColor = PanelColor.BackColor;
            ToolStripProgressBarWidth.ForeColor = PanelColor.BackColor;
        }
        /// <summary>
        /// Perfoms drawing while mouse moves over the PictureBox and left mouse button is pressed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PictureBoxMouseMove(object sender, MouseEventArgs e)
        {
            ToolStripStatusLabelCoordinates.Text = @"Coordinates(X,Y): " + e.X + @"*" + e.Y;
            if (e.Button != MouseButtons.Left) return;
            //var graphics = Graphics.FromImage(_bitmap);//PictureBox.CreateGraphics();//Graphics.FromHwnd(PictureBox.Handle);
            _graphics.FillRectangle(_solidBrush, e.X, e.Y, _xSize, _xSize);
            PictureBox.Refresh();
        }
        /// <summary>
        /// Clears the canvas
        /// </summary>
        /// <param name="bitmap"></param>
        private void ClearAndSet(Image bitmap)
        {
            _bitmap = new Bitmap(bitmap, PictureBox.Width, PictureBox.Height);
            PictureBox.Image = _bitmap;
            _graphics = Graphics.FromImage(_bitmap);
        }
        /// <summary>
        /// Setting width to 1px
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadioButton1PxCheckedChanged(object sender, EventArgs e)
        {
            if (!RadioButton1Px.Checked) return;
            _xSize = 1;
            ToolStripProgressBarWidth.Value = _xSize * 10;
            ToolStripProgressBarWidth.ForeColor = _solidBrush.Color;
        }
        /// <summary>
        /// Setting width to 2px
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadioButton2PxCheckedChanged(object sender, EventArgs e)
        {
            if (!RadioButton2Px.Checked) return;
            _xSize = 2;
            ToolStripProgressBarWidth.Value = _xSize * 10;
            ToolStripProgressBarWidth.ForeColor = _solidBrush.Color;
        }
        /// <summary>
        /// Setting width to 4px
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadioButton4PxCheckedChanged(object sender, EventArgs e)
        {
            if (!RadioButton4Px.Checked) return;
            _xSize = 4;
            ToolStripProgressBarWidth.Value = _xSize * 10;
            ToolStripProgressBarWidth.ForeColor = _solidBrush.Color;
        }
        /// <summary>
        /// Setting width to 10px
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadioButton10PxCheckedChanged(object sender, EventArgs e)
        {
            if (!RadioButton10Px.Checked) return;
            _xSize = 10;
            ToolStripProgressBarWidth.Value = _xSize * 10;
            ToolStripProgressBarWidth.ForeColor = _solidBrush.Color;
        }
        /// <summary>
        /// Opening an image
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuStripItemOpenClick(object sender, EventArgs e)
        {
            if (OpenFileDialog.ShowDialog() != DialogResult.OK) return;
            try
            {
                var psd = new PsdFile();
                psd.Load(OpenFileDialog.OpenFile());
                var imageStream = new MemoryStream();
                psd.Save(imageStream);
                ClearAndSet(new Bitmap(imageStream));
                PictureBox.Refresh();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, @"Error while opening the image",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Saving image to a file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuStripItemSaveAsClick(object sender, EventArgs e)
        {

        }

        private void PictureBoxDoubleClick(object sender, EventArgs e)
        {
            if (RadioButton1Px.Checked)
            {
                RadioButton2Px.Checked = true;
                _xSize = 2;
            }
            else if (RadioButton2Px.Checked)
            {
                RadioButton4Px.Checked = true;
                _xSize = 4;
            }
            else if (RadioButton4Px.Checked)
            {
                RadioButton10Px.Checked = true;
                _xSize = 10;
            }
            else if (RadioButton10Px.Checked)
            {
                RadioButton1Px.Checked = true;
                _xSize = 1;
            }
        }

        private void ActionClearExecute(object sender, EventArgs e)
        {
            ClearAndSet(new Bitmap(PictureBox.Width, PictureBox.Height));
            _graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, PictureBox.Width, PictureBox.Height);
            PictureBox.Refresh();
        }

        private void ActionColorExecute(object sender, EventArgs e)
        {
            ColorDialog = new ColorDialog { Color = _solidBrush.Color };
            ColorDialog.ShowDialog();
            _solidBrush.Color = ColorDialog.Color;
            PanelColor.BackColor = _solidBrush.Color;
        }

        private void ActionExitExecute(object sender, EventArgs e)
        {
            _solidBrush.Dispose();
            _graphics.Dispose();
            _bitmap.Dispose();
            Environment.Exit(0);
        }

        private void ActionAboutExecute(object sender, EventArgs e)
        {
            MessageBox.Show(Resources.DrawingForm_MenuStripItemAboutClick_,
                                @"About", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ActionSaveAsExecute(object sender, EventArgs e)
        {
            if (SaveFileDialog.ShowDialog() != DialogResult.OK) return;
            var fs = SaveFileDialog.OpenFile();
            fs.Close();
            //Saver.GetInstance().Save(SaveFileDialog.FileName, , PictureBox.Image);
        }
    }
}
